package com.ark.cloud.javacallnative.modules.storage.service.impl;

import com.ark.cloud.javacallnative.entity.*;
import com.ark.cloud.javacallnative.modules.storage.mapper.*;
import com.ark.cloud.javacallnative.modules.storage.service.ContactStateService;
import com.ark.cloud.javacallnative.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author fukun
 *
 * @create 2021/9/2  上午11:01
 * @description 目标数据

 */
@Service
public class ContactStateServiceImpl implements ContactStateService {

    @Autowired
    ContactStateMapper contactStateDao;
    @Autowired
    DetectingRecordMapper detectingRecordDao;
    @Autowired
    SensorInfoMapper sensorInfoDao;
    @Autowired
    ContactStateDetectingRecordMapper contactStateDetectingRecordDao;
    @Autowired
    DetectingRecordSensorInfoMapper detectingRecordSensorInfoDao;

    @Override
    public void saveContactStateInfo(ContactState contactState) {

        // 设置UUID
        contactState.setCid(UUIDUtil.next());
        System.out.println("contactState:"+contactState);

        contactStateDao.saveContactStateInfo(contactState);

        Long contactStateCid = contactState.getCid();

//
        ArrayList<DetectingRecord> recordList = contactState.getRecordList();

        for (DetectingRecord detectingRecord : recordList) {

            detectingRecord.setId(UUIDUtil.next());

            detectingRecordDao.insert(detectingRecord);

            Long detectingRecordId = detectingRecord.getId();

//
            ContactStateDetectingRecord contactStateDetectingRecord = new ContactStateDetectingRecord();

            contactStateDetectingRecord.setContactStateId(contactStateCid);
            contactStateDetectingRecord.setDetectingRecordId(detectingRecordId);

            contactStateDetectingRecordDao.insert(contactStateDetectingRecord);

            ArrayList<SensorInfo> sensorList = detectingRecord.getSensorList();

            for (SensorInfo sensorInfo : sensorList) {

                 sensorInfo.setSensorInfoId(UUIDUtil.next());

                sensorInfoDao.saveSensorInfo(sensorInfo);

                Long sensorInfoId = sensorInfo.getSensorInfoId();

                DetectingRecordSensorInfo detectingRecordSensorInfo = new DetectingRecordSensorInfo();

//                设置UUID
                detectingRecordSensorInfo.setId(UUIDUtil.next());
                detectingRecordSensorInfo.setSensorInfoId(sensorInfoId);
                detectingRecordSensorInfo.setDetectingRecordId(detectingRecordId);

                detectingRecordSensorInfoDao.insert(detectingRecordSensorInfo);

            }
        }
    }
}
